/*
 * Created on 2006-4-5
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.sitech.portal.system.action;

import java.util.HashMap;

import com.sitech.ibnms.core.IPage;
import com.sitech.ibnms.core.JGridBaseAction;
import com.sitech.portal.system.form.SysParaForm;
import com.sitech.portal.system.service.ISysParaService;

/**
 * @author niezl
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class SysParaAction extends JGridBaseAction {
	
	private SysParaForm theForm = new SysParaForm();
	private ISysParaService sysParaService;
	private String flag;

	public SysParaForm getTheForm() {
		return theForm;
	}

	public void setTheForm(SysParaForm theForm) {
		this.theForm = theForm;
	}

	public ISysParaService getSysParaService() {
		return sysParaService;
	}

	public void setSysParaService(ISysParaService sysParaService) {
		this.sysParaService = sysParaService;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public String loadListSysPara() {
		return SUCCESS;
	}
	
	public String loadListSysParaQueryJson() {
		IPage result = sysParaService.findListSysPara(this.getPageForm());
		this.setResult(result);
		return SUCCESS;
	}

	public String editSysPara() {
		if (theForm.getISMP_JSP_COMMAND() != null	&& theForm.getISMP_JSP_COMMAND().equals("add")) {
			theForm.setPARA_ID(null);
			return SUCCESS;

		} else if (theForm.getISMP_JSP_COMMAND() != null && theForm.getISMP_JSP_COMMAND().equals("update")) {
			String PARA_ID = theForm.getPARA_ID();
			HashMap map = sysParaService.findObjForTB_SYS_PARAByPK(PARA_ID);
			theForm.setPARA_NAME((String) map.get("PARA_NAME"));
			theForm.setPARA_VALUE((String) map.get("PARA_VALUE"));
			theForm.setPARA_DESC((String) map.get("PARA_DESC"));
			theForm.setENABLE((String) map.get("ENABLE"));
			theForm.setPARA_ID(PARA_ID);
		}

		return SUCCESS;
	}

	public String saveSysPara() {
		flag = "true";
		
		try{
			HashMap map = new HashMap();
			map.put("PARA_ID", theForm.getPARA_ID());
			map.put("PARA_NAME", theForm.getPARA_NAME());
			map.put("PARA_VALUE", theForm.getPARA_VALUE());
			map.put("PARA_DESC", theForm.getPARA_DESC());
			map.put("ENABLE", theForm.getENABLE());
			if (theForm.getISMP_JSP_COMMAND() != null
					&& theForm.getISMP_JSP_COMMAND().equals("add")) {
				
				HashMap paraMap = sysParaService.findSysParaByParaId(theForm.getPARA_ID());
				if(paraMap!=null)
					throw new Exception("参数ID已存在，请重新输入");
				sysParaService.add(map);
			} else if (theForm.getISMP_JSP_COMMAND() != null
					&& theForm.getISMP_JSP_COMMAND().equals("update")) {
				sysParaService.update(map);
			}
		}catch(Exception e){
			flag = e.getMessage();
		}

		return SUCCESS;
	}

	public String delSysPara() {
		flag = "true";
		String PARA_ID = theForm.getPARA_ID();
		try {
			sysParaService.deleteForTB_SYS_PARAByPK(PARA_ID);
		} catch (Exception e) {
			flag = e.getMessage();
		}

		return SUCCESS;
	}
}